<?php
require_once "../config.php";
require_once "../include/function.php"; 

switch($_GET['action']){
	case 'check':
	if($_POST['verifycode']!=$_SESSION['verify_code']){
		$msg = "验证码不正确，请重新登陆";
	}else{
		$username = $_POST['username'];
		$password = MD5($_GLOBAS['salt'].$_POST['password']);
		
		$query = "SELECT * FROM user WHERE (level=1 AND username='".$username."' AND password='".$password."');";
		$result = mysql_query($query);
		
		if($line = mysql_fetch_array($result, MYSQL_ASSOC)){
			$query = "UPDATE user SET lastlogin=NOW() WHERE username='".$line['username']."';";
			mysql_query($query);
			
			$_SESSION['admin'] = $line['username'];
			header('Location: index.php');
		}else $msg = "帐号或密码错误，请重新登陆";
	}
	break;
	case 'logout':
		unset($_SESSION['admin']);
		break;
	default:
		if(isset($_SESSION['admin'])) header('Location: index.php');
}?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html>
<head>
	<meta http-equiv="content-type" content="text/html; charset=UTF-8">
	<title>拾客>管理平台>登陆</title>
	<script type="text/javascript" src="../include/jquery-1.4.4.min.js"></script> 
	<script type="text/javascript" src="../include/jquery-ui-1.8.6.custom.min.js"></script> 
	<link type="text/css" href="../css/custom-theme/jquery-ui-1.8.6.custom.css" rel="stylesheet" /> 
	<link type="text/css" href="css/default.css" rel="stylesheet" />
	<script> 
	$(function(){
		// check the register form
		
		username = /^[A-Za-z][A-Za-z0-9]{4,11}$/i;
		verifycode = /^[0-9]{4}$/i;
		
		$("#username").blur(function(){
			$("#checkusername").text((!username.test($(this).val()))?"请输入正确的帐号！":"OK");
		});
		$("#password").blur(function(){
			$("#checkpassword").text(($(this).val().length<6)?"密码至少6位":"OK");
		});
		$("#verifycode").blur(function(){
			$("#checkverifycode").text((!verifycode.test($(this).val()))?"验证码为4位数字！":"OK");
		});
		$("#loginbtn").click(function(){
			if(!verifycode.test($("#verifycode").val())){$("#verifycode").blur().focus();return false;}
			if(!username.test($("#username").val())){$("#username").blur().focus();return false;}
			if($("#password").val().length<6){$("#password").blur().focus();return false;}
			return true;
		});
	});
	function changecode(){
		var vimg = document.getElementById("verifyimg");
		vimg.src = "include/verify.php";
	}
	</script>
	<style>
		.grey{background-color: #EEE;}
		#box{width: 560px; margin: 0 auto; position:relative; }
		#sidebar{text-align: center; position: absolute; width: 120px; top: 0; bottom:0; background: #2C89A0 url(images/logo.png) no-repeat center top;}
		#sidebar h1{margin: 140px 0 0;}
		#copyright{position: absolute; bottom: 20px; width: 100%; color: #FFF;}
		#login{width: 420px; float: right; padding-top: 20px;}
		h1{font-size: 42px; line-height: 42px; color: #FFF;}
		h2{margin-bottom: 10px;}
		h3{border-bottom: 1px solid #999; padding-bottom: 5px; margin-bottom: 5px;}
		.formtable{margin: 10px 0; border-spacing: 5px 10px;}
		.formtable tr{}
		.formtable th{width: 100px; text-align: right; vertical-align: top;}
		.formtable input{border: 1px solid #CCC; margin-right: 5px;}
		.formtable .tip{font-size: 12px; color: black;}
		.warning{font-size:12px; color: red;}
		#rule{overflow: auto; height: 10em;  border: 1px solid #CCC; font-size: 12px; margin-bottom: 10px;}
		#loginbtn{border: none; background: #F60; padding: 5px 10px; color: #FFF;}
	</style>
</head>
<body>
<div id="header" class="grey">
	<div id="container">
		<div id="box">
			<div id="sidebar">
				<h1>拾客</h1>
				<div id="copyright">&copy; 2011</div></div>
			<div id="login"><h2>管理平台</h2>
				<h3>请登录</h3>
				<p class="warning"><?php if(isset($msg)) echo $msg;?></p>
				<form action="?action=check" method="post">
				<table class="formtable" >
					<tr><th>管理员</th><td><input type="text" name="username" id="username" />
						<span id="checkusername" class="warning"></span></td></tr>
					<tr><th>密码</th><td><input type="password" name="password" id="password" />
						<span id="checkpassword" class="warning"></span></td></tr>
					<tr><th>验证码</th><td><img src="../include/verify.php" id="verifyimg" />
						<p class="tip">请输入图中的数字</p>
						<input type="text" name="verifycode" id="verifycode"/>
						<span id="checkverifycode" class="warning"></span></td></tr>
					<tr><th></th><td><input type="submit" value="登陆" id="loginbtn" /></td></tr>
			</table></form>
			</div>
			<div class="clearb"></div>
		</div>
	</div>
</div>
</body>
</html>
<?php 
mysql_free_result($result);
mysql_close($conn); ?>